package com.stylefeng.guns.gateway.modular.user;

import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.stylefeng.guns.api.user.UserAPI;
import com.stylefeng.guns.api.user.vo.UserInfoModel;
import com.stylefeng.guns.api.user.vo.UserModel;
import com.stylefeng.guns.gateway.common.CurrentUser;
import com.stylefeng.guns.gateway.modular.vo.ResponseVO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user/")
@RestController
public class UserController {
    @Reference
    private UserAPI userAPI;

    @RequestMapping("register")
    public ResponseVO register(UserModel userModel) {
        if (StrUtil.isBlank(userModel.getUsername())) {
            return ResponseVO.serviceFail("用户名不能为空");
        }
        if (StrUtil.isBlank(userModel.getPassword())) {
            return ResponseVO.serviceFail("密码不能为空");
        }
        boolean isSuccess = userAPI.register(userModel);
        if (isSuccess) {
            return ResponseVO.success("注册成功");
        }


        return ResponseVO.serviceFail("注册失败");
    }

    @RequestMapping("check")
    public ResponseVO check(String username) {
        if (StrUtil.isNotBlank(username)) {
            boolean notExists = userAPI.checkUsername(username);
            if (notExists) {
                return ResponseVO.success("用户名不存在");
            } else {
                return ResponseVO.serviceFail("用户名已存在");
            }
        } else {
            return ResponseVO.serviceFail("用户名不能为空");
        }

    }

    @RequestMapping("getUserInfo")
    public ResponseVO getUserInfo() {
        String userId = CurrentUser.getCurrentUser();
        if (StrUtil.isNotBlank(userId)) {
            int uuid = Integer.parseInt(userId);
            UserInfoModel userInfo = userAPI.getUserInfo(uuid);
            if (userInfo != null) {
                return ResponseVO.success(userInfo);
            } else {
                return ResponseVO.appFail("用户信息查询失败");
            }
        } else {
            return ResponseVO.serviceFail("用户未登陆");
        }


    }
 @RequestMapping("updateUserInfo")
    public ResponseVO updateUserInfo(UserInfoModel userInfoModel) {
        String userId = CurrentUser.getCurrentUser();
        if (StrUtil.isNotBlank(userId)) {
            int uuid = Integer.parseInt(userId);

            if (uuid != userInfoModel.getUuid()){
                return ResponseVO.serviceFail("请修改您个人的信息");
            }
            UserInfoModel userInfo = userAPI.updateUserInfo(userInfoModel);
            if (userInfo != null) {
                return ResponseVO.success(userInfo);
            } else {
                return ResponseVO.appFail("用户信息查询失败");
            }
        } else {
            return ResponseVO.serviceFail("用户未登陆");
        }
    }


    /**
     * 应用:
     * 1. 前端存储JWT(7天): JWT的刷新
     * 2. 服务器会存储活动用户信息(30分钟)
     * 3. JWT里的userId为key,查找活跃用户
     * 退出:
     * 1. 前端删除掉JWT
     * 2. 后端服务器删除活跃用户缓存
     * 现状:
     * 1. 前端删除掉JWT
     *
     * @return
     */
    @RequestMapping("logout")
    public ResponseVO logout() {
        return ResponseVO.success("用户退出成功");
    }

}
